<script setup>
import { ref, watch, onUnmounted, onMounted } from 'vue'
import Sightsee from './sightsee/index.vue'
import MannedCommute from './manned-commute/index.vue'
import Transport from './transport/index.vue'
import Patrol from './patrol/index.vue'
import FlyVerify from './fly-verify/index.vue'
import { useAirStore } from '@/stores'
import { storeToRefs } from 'pinia'
import AirAnimation from './js/airAnimation'
import { operationOverviewServer } from '@/api'
import { addHangxian, clear, addSelfLine } from './js/sightsee'
import { addZXJ } from './js/zwxj'
import { addZRTQ } from './js/zrtq'

const airAnimation = new AirAnimation()
const airStore = useAirStore()
const { AirFlight, FocusAirId, operationData } = storeToRefs(airStore)
const list = ref(['文旅观光', '载人通勤', '物流运输', '政务巡查', '飞行验证'])
const flyType = ref(1)
const types = {
  文旅观光: 1,
  载人通勤: 2,
  物流运输: 3,
  政务巡查: 4,
  飞行验证: 5
}
const currentBar = ref(list.value[0])
const chosedBar = (item) => {
  clear()
  clearInterval(window.timer)
  airAnimation.clerpolyline()
  if (currentBar.value == item) return
  flyType.value = types[item]
  FocusAirId.value = ''
  currentBar.value = item
  get0perationOverview()
  if (flyType.value == 1) {
    __g.camera.set(539974.50625, 2490663.662168, 6212.273125, -20.365793, -129.461304, 2)
  } else if (flyType.value == 2) {
    // addSelfLine()
    addZRTQ()
    __g.camera.set([533437.281406, 2492161.00875, 7491.65, -40.971615, -113.303406, 1])
  }
  if (flyType.value == 3) {
    addSelfLine()
    __g.camera.set([532936.009062, 2489525.396562, 8794.779375, -36.890621, -123.807838, 2])
  } else if (flyType.value == 5) {
    __g.camera.set([536342.642813, 2496052.030312, 1716.147656, -12.002158, -129.854507, 2])
  } else if (flyType.value === 4) {
    // addHangxian(4)
    addZXJ()
    __g.camera.set(529073.623437, 2493059.927812, 3165.890938, -26.114782, -129.671082, 2)
  }
}

// 获取运营概况信息
const get0perationOverview = async () => {
  airAnimation.clearAirs()
  const { data } = await operationOverviewServer({ flyType: flyType.value })
  operationData.value = data
}

watch(
  AirFlight,
  (newVal) => {
    let newList = {}
    for (let i in newVal) {
      if (newVal[i].flyType == flyType.value) {
        newList[i] = newVal[i]
      }
    }
    //飞行验证不添加，用模拟数据
    if (flyType.value != 5) {
      airAnimation.startMove(newList)
    }
  },
  {
    deep: true,
    immediate: true
  }
)

onUnmounted(() => {
  airAnimation.clearAirs()
  __g.polyline.clear()
  clearInterval(window.timer)
})

onMounted(() => {
  flyType.value = 1
  if (flyType.value == 1) {
    __g.camera.set([537243.182344, 2492013.599687, 7863.095, -31.651682, -128.043091, 2])
  }
  if (flyType.value == 5) {
    __g.camera.set([536342.642813, 2496052.030312, 1716.147656, -12.002158, -129.854507, 2])
  } else {
    __g.camera.set(537243.182344, 2492013.599687, 7863.095, -31.651682, -128.043091, 2)
  }
})
</script>
<template>
  <MannedCommute v-if="currentBar == '文旅观光'" />
  <Sightsee v-if="currentBar == '载人通勤'" />
  <Transport v-if="currentBar == '物流运输'" />
  <Patrol v-if="currentBar == '政务巡查'" />
  <FlyVerify v-if="currentBar == '飞行验证'" />
  <BottomBar :list="list" @chosedBar="chosedBar" />
</template>
<style scoped lang="scss"></style>
